package com.google.code.p.ivans;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cafe.ejb.WaiterService;
import cafe.ejb.myexceptions.EntityNotFoundException;
import cafe.ejb.serviceinterfaces.WaiterServiceInterface;
import cafe.jpa.Waiter;

public class SearchWaiter extends HttpServlet {	

	/**
	 * 
	 */
	private static final long serialVersionUID = -2925563533970242596L;
	
	@EJB 
	private WaiterService waiterSI;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
		List<Waiter> waiters;
		
		String firstName = new String(request.getParameter("firstName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
		
		String lastName = new String(request.getParameter("lastName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
		
		String patronymicName = new String(request.getParameter("patronymicName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
		
		if(firstName.equals("")) {
			if(lastName.equals("")) {				
				if(patronymicName .equals("")) {				
										
				}				
				else  {					
					try {
						waiters = waiterSI.getAllWaitersByName(WaiterServiceInterface.SEARCH_BY_PATRONYMICNAME, patronymicName);
						request.setAttribute("waiters", waiters);
					} catch (EntityNotFoundException e) {
						
					} catch (SQLException e) {
						
					}
				}
			}
			
			else if(patronymicName .equals("")) {
				
				try {
					waiters = waiterSI.getAllWaitersByName(WaiterServiceInterface.SEARCH_BY_LASTNAME, lastName);
					request.setAttribute("waiters", waiters);
				} catch (EntityNotFoundException e) {
					
				} catch (SQLException e) {
					
				}
			}
			
			else {
				try {
					waiters = waiterSI.getAllWaitersByName(WaiterServiceInterface.SEARCH_BY_LASTNAME_AND_PATRONYMICNAME,lastName, patronymicName);
					request.setAttribute("waiters", waiters);
				} catch (EntityNotFoundException e) {
					
				} catch (SQLException e) {
					
				}
			}
		}
		else if(lastName.equals("")) {			
			if(patronymicName .equals("")) {
				try {
					waiters = waiterSI.getAllWaitersByName(WaiterServiceInterface.SEARCH_BY_FIRSTNAME, firstName);
					request.setAttribute("waiters", waiters);
				} catch (EntityNotFoundException e) {
					
				} catch (SQLException e) {
					
				}
			}			
			else {
				try {
					waiters = waiterSI.getAllWaitersByName(WaiterServiceInterface.SEARCH_BY_FIRSTNAME_AND_PATRONYMICNAME, firstName, patronymicName);
					request.setAttribute("waiters", waiters);
				} catch (EntityNotFoundException e) {
					
				} catch (SQLException e) {
					
				}
			}
			
		}		
		else if(patronymicName.equals("")) {
			try {
				waiters = waiterSI.getAllWaitersByName(WaiterServiceInterface.SEARCH_BY_FIRSTNAME_AND_LASTNAME, firstName, lastName);
				request.setAttribute("waiters", waiters);
			} catch (EntityNotFoundException e) {
				
			} catch (SQLException e) {
				
			}
		}		
		
		else {
			try {
				waiters = waiterSI.getAllWaitersByName(WaiterServiceInterface.SEARCH_BY_ALL, firstName, lastName, patronymicName);
				request.setAttribute("waiters", waiters);
			} catch (EntityNotFoundException e) {
				
			} catch (SQLException e) {
				
			}
		}		
		
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("searchWaitersResult.jsp");		
		requestDispatcher.forward(request, response);
	}
}
